<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<html>
<head>


<script type="text/javascript">
	var listUrl = "rest/userRoleList.do";
	var editUserRoleUrl = "rest/userRoleEdit.do";

$(document).ready(function() {
		

		$("#role-dg").datagrid({
			loadMsg : '数据加载中....',
			title : '分配角色',
			rownumbers : true,
// 		    height:'90%',
			width : '100%',
			url : listUrl + "?userId=${userId}",
			nowrap : false,
			striped : true,
			sortName : 'id',
			sortOrder : 'asc',
			remoteSort : false,
			selectOnCheck : true,
			checkOnSelect : true,
			singleSelect : false,

			columns : [ 
               [
		              {field : 'id',checkbox : true},
		              {field : 'rolename',title : '角色名称',width : 60},
					  {field : 'roledesc',title : '角色描述',width : 100} 
               ] 
			],
			onLoadSuccess : function(data) {
				if (data) {
					$.each(data.rows, function(index, item) {
						if (item.checked) {
							$('#role-dg').datagrid('checkRow',index);
						}
					})
				}
			}
		});// end datagrid
	})//end ready

	function back(url) {
		$.get(url, {}, function(data) {
			$("#center").empty();
			$("#center").html(data);
			$.parser.parse($("#center"));
		});
	}

	function editUserRole() {
		var url = editUserRoleUrl;
		var checkedItems = $('#role-dg').datagrid('getChecked');
		var ids = [];
		$.each(checkedItems, function(index, item) {
			ids.push(item.id);
		});
		ids = ids.join(',');
		$.post(url, {
			userId : "${userId}",
			ids : ids
		},function(result) {
			//var result = eval('(' + result + ')');
            if (result.success=='true') {
                back("user/index.do");
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result.msg
                });
            }
		});

	}
</script>
</head>
<body>


	<div class="linkbar"><a>系统</a>/<a>分配角色</a></div>
	
	<div class="clear"></div>
	
	<div id="role-dg" toolbar="#toolbar"></div>

	<div id="toolbar">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" plain="true"
			onclick="editUserRole()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-undo" plain="true" onclick="back('user/index.do')">返回上级菜单</a>
	</div>


</body>
</html>